package com.cpj.bsyyzx.controller;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpj.bsyyzx.entity.*;
import com.cpj.bsyyzx.mapper.FileNumMapper;
import com.cpj.bsyyzx.service.IAccountService;
import com.cpj.bsyyzx.service.IExitRegistrationService;
import com.cpj.bsyyzx.service.IFileNumService;
import com.cpj.bsyyzx.service.IUserService;
import com.cpj.bsyyzx.util.CommonResult;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 五个臭皮匠
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/exitRegistration")
@Log4j
public class ExitRegistrationController {
    @Resource
    FileNumMapper fileNumMapper;
    @Resource
    IFileNumService fileNumService;
    @Resource
    IExitRegistrationService exitRegistrationService;
    @Autowired
    UserController userController;
    @Autowired
    IUserService userService;
    @Autowired
    IAccountService accountService;
    @RequestMapping("/pagelist")
    CommonResult pagelist(Page page){
        QueryWrapper<ExitRegistration> queryWrapper = new QueryWrapper<>();
        log.debug("分页查询");
        IPage<ExitRegistration> rpage=exitRegistrationService.selectPageVo(page,queryWrapper);
        return CommonResult.success(rpage);
    }
    @RequestMapping("/list")
    CommonResult list(Page page, ExitRegistration exitRegistration){
        QueryWrapper<ExitRegistration> queryWrapper = new QueryWrapper<>();
        log.debug("分页查询");
        Page<ExitRegistration> rpage=exitRegistrationService.pageList(page,exitRegistration);
        return CommonResult.success(rpage);
    }
    @RequestMapping("/add")
    @ResponseBody
    CommonResult add(ExitRegistration exitRegistration) {
        FileNum fileNum = fileNumMapper.selectFileByUser();
        Integer list = fileNum.getMaxnum();
        list += 1;
        fileNum.setMaxnum(list);
        fileNumService.saveOrUpdate(fileNum);
        exitRegistration.setFileNum("exRegister" + list);

        return CommonResult.success(exitRegistrationService.save(exitRegistration));

    }
    @RequestMapping("save")
    @ResponseBody
    CommonResult save(ExitRegistration exitRegistration) {
        log.debug("保存");
        exitRegistration.setRequestTime(LocalDateTime.now());
        boolean success = exitRegistrationService.saveOrUpdate(exitRegistration);
        return  CommonResult.success(success);
    }
    @RequestMapping("toedit")
    @ResponseBody
    CommonResult toedit(ModelMap modelMap, Integer id) {
        log.debug("跳转到修改页面");
//        User user = userService.getById(id);
//        modelMap.addAttribute("user", user);
        return CommonResult.success(exitRegistrationService.getById(id));
    }
    @RequestMapping("del/{id}")
    @ResponseBody
    CommonResult delete(@PathVariable Integer id) {
        log.debug("根据主键删除:" + id);
        boolean success = exitRegistrationService.removeById(id);
        return CommonResult.success(success);
    }

    @RequestMapping("approvalSave")

    CommonResult approvalSave(ExitRegistration exitRegistration,HttpServletRequest request){
        System.out.println(exitRegistration);
        if ("通过".equals(exitRegistration.getStatus())){
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("file_num",exitRegistration.getFileNum());
            User user= userService.getOne(wrapper);
            if ("保留床位".equals(exitRegistration.getExitType())){
                userController.deleteByRetainBed(user.getUserId());
            }
            else {
                userController.deleteDoing(user.getUserId());
            }
        }
        //审批人为当前登录用户
        String token = request.getHeader("token");
        JWTVerifier verifier= JWT.require(Algorithm.HMAC256("loginUser")).build();
        DecodedJWT jwt =verifier.verify(token);
        Integer accId = jwt.getClaim("accId").asInt();
        Account account=accountService.getById(accId);
        exitRegistration.setAuditor(account.getStaffName());
        //审批时间为当前时间
        exitRegistration.setAuditTime(LocalDateTime.now());
        boolean success=exitRegistrationService.saveOrUpdate(exitRegistration);
        return CommonResult.success(success);
    }

}
